"""
Pydantic 数据模型

定义 API 请求和响应的数据结构
"""

from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime
from enum import Enum


# ========== 枚举类型 ==========

class FileType(str, Enum):
    """文件类型"""
    IMAGE = "image"
    DOCUMENT = "document"
    VIDEO = "video"
    AUDIO = "audio"
    OTHER = "other"


class FileStatus(str, Enum):
    """文件状态"""
    UPLOADING = "uploading"
    COMPLETED = "completed"
    PROCESSING = "processing"
    FAILED = "failed"
    DELETED = "deleted"


# ========== 缩略图模型 ==========

class ThumbnailResponse(BaseModel):
    """缩略图响应模型"""
    id: int
    size_name: str = Field(..., description="尺寸名称")
    filename: str = Field(..., description="文件名")
    file_path: str = Field(..., description="文件路径")
    width: int = Field(..., description="宽度")
    height: int = Field(..., description="高度")
    file_size: int = Field(..., description="文件大小（字节）")
    url: Optional[str] = Field(None, description="访问 URL")
    
    class Config:
        from_attributes = True


# ========== 文件上传模型 ==========

class FileUploadResponse(BaseModel):
    """文件上传响应模型"""
    id: int
    filename: str = Field(..., description="原始文件名")
    stored_filename: str = Field(..., description="存储文件名")
    file_type: FileType = Field(..., description="文件类型")
    file_size: int = Field(..., description="文件大小（字节）")
    file_size_formatted: Optional[str] = Field(None, description="格式化的文件大小")
    mime_type: Optional[str] = Field(None, description="MIME 类型")
    file_hash: Optional[str] = Field(None, description="文件哈希值")
    
    # 图片专用
    width: Optional[int] = Field(None, description="图片宽度")
    height: Optional[int] = Field(None, description="图片高度")
    
    # 状态
    status: FileStatus = Field(..., description="文件状态")
    
    # 元数据
    description: Optional[str] = Field(None, description="文件描述")
    tags: Optional[str] = Field(None, description="标签")
    is_public: bool = Field(default=False, description="是否公开")
    
    # 时间
    created_at: datetime = Field(..., description="上传时间")
    updated_at: datetime = Field(..., description="更新时间")
    
    # URL
    url: Optional[str] = Field(None, description="文件访问 URL")
    download_url: Optional[str] = Field(None, description="下载 URL")
    
    # 缩略图（仅图片）
    thumbnails: Optional[List[ThumbnailResponse]] = Field(None, description="缩略图列表")
    
    class Config:
        from_attributes = True


class FileUpdate(BaseModel):
    """文件更新模型"""
    description: Optional[str] = Field(None, max_length=500, description="文件描述")
    tags: Optional[str] = Field(None, max_length=500, description="标签")
    is_public: Optional[bool] = Field(None, description="是否公开")


# ========== 文件列表模型 ==========

class FileListItem(BaseModel):
    """文件列表项"""
    id: int
    filename: str
    file_type: FileType
    file_size: int
    file_size_formatted: str
    status: FileStatus
    is_public: bool
    created_at: datetime
    url: Optional[str] = None
    thumbnail_url: Optional[str] = None  # 仅图片有缩略图
    
    class Config:
        from_attributes = True


class FileListResponse(BaseModel):
    """文件列表响应"""
    total: int = Field(..., description="总数")
    items: List[FileListItem] = Field(..., description="文件列表")
    page: int = Field(..., description="当前页")
    page_size: int = Field(..., description="每页数量")
    total_pages: int = Field(..., description="总页数")


# ========== 图片处理请求模型 ==========

class ThumbnailCreateRequest(BaseModel):
    """创建缩略图请求"""
    sizes: List[str] = Field(default=["small", "medium", "large"], description="要创建的尺寸")


class ImageCropRequest(BaseModel):
    """图片裁剪请求"""
    left: int = Field(..., ge=0, description="左边界")
    top: int = Field(..., ge=0, description="上边界")
    right: int = Field(..., gt=0, description="右边界")
    bottom: int = Field(..., gt=0, description="下边界")


class ImageResizeRequest(BaseModel):
    """图片调整大小请求"""
    width: int = Field(..., gt=0, le=4000, description="宽度")
    height: int = Field(..., gt=0, le=4000, description="高度")
    keep_aspect_ratio: bool = Field(default=True, description="保持宽高比")


class ImageWatermarkRequest(BaseModel):
    """图片水印请求"""
    text: str = Field(..., max_length=100, description="水印文字")
    position: str = Field(default="bottom-right", description="位置")
    opacity: int = Field(default=128, ge=0, le=255, description="不透明度")
    font_size: int = Field(default=36, ge=10, le=100, description="字体大小")


class ImageFilterRequest(BaseModel):
    """图片滤镜请求"""
    filter_type: str = Field(..., description="滤镜类型")


class ImageRotateRequest(BaseModel):
    """图片旋转请求"""
    angle: int = Field(..., ge=-360, le=360, description="旋转角度")
    expand: bool = Field(default=True, description="是否扩展画布")


# ========== 通用响应模型 ==========

class MessageResponse(BaseModel):
    """通用消息响应"""
    message: str = Field(..., description="响应消息")


class FileStatsResponse(BaseModel):
    """文件统计响应"""
    total_files: int = Field(..., description="总文件数")
    total_size: int = Field(..., description="总大小（字节）")
    total_size_formatted: str = Field(..., description="格式化的总大小")
    by_type: dict = Field(..., description="按类型统计")
    by_status: dict = Field(..., description="按状态统计")

